package sputils

import (
	"fmt"
	"gitee.com/ymofen/gobase"
	"reflect"
)

func SpConnStringFromSubSessionString(v0 string) (newstr string) {
	strMap := gobase.NewStrMap()
	strMap.URLFormDecode(v0)
	conntype := strMap.StringByName("conntype", "")
	if conntype == "ntripserv" {
		// conntype=ntripserv&connstr=10.244.4.92:9007&mountpoint=NS01&auth=0000&proxydial=
		// conntype=ntripserv&connstr=127.0.0.1:5002&mountpoint=HB01&auth=0000&proxydial=
		return v0
	} else if conntype == "caster" {
		// "conntype=caster&addr=:9007&ntripcli.maxonline=0&mountpoint=HB01&mpflag=1&maxonline=0&auth=001:123"
		//  conntype=caster.pub.ntripclient&connstr=:9007&mountpoint=HB01&mpflag=1&maxonline=0&auth=001:123
		strMap["conntype"] = "caster.pub.ntripclient"
		strMap["connstr"] = strMap["addr"]
		strMap["maxonline"] = strMap["ntripcli.maxonline"]
		delete(strMap, "addr")
		delete(strMap, "ntripcli.maxonline")
	} else if conntype == "tcpserver" {
		// conntype=tcpserver&connstr=0.0.0.0%3A9100&maxonline=1
		// conntype=tcpserver&addr=:10000&network=tcp4
		strMap["conntype"] = "natstcpserver"
		strMap["addr"] = strMap["connstr"]
		strMap["maxonline"] = strMap["ntripcli.maxonline"]
		delete(strMap, "connstr")
	} else if conntype == "tcpclient" {

	}
	return strMap.URLFormEncode()
}

func CheckIsNil(obj interface{}) bool {
	if obj == nil {
		return true
	}
	v := reflect.ValueOf(obj)
	switch v.Kind() {
	case reflect.Map, reflect.Slice, reflect.Chan, reflect.Func, reflect.Pointer, reflect.UnsafePointer, reflect.Interface:
		return v.IsNil()
	default:
		return false
	}
}

func ObjectType(obj interface{}) string {
	typ := reflect.TypeOf(obj)
	if typ == nil {
		return "<null>"
	}
	name := typ.Name()
	if len(name) == 0 {
		return fmt.Sprintf("%v", typ)
	}
	return name
}
